home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Microsoft Plateform / Visual Basic 5.0 / Msvb50.ace / msvb50 / MSVB50 / VB / TEMPLATE / FORMS / QUERYS.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-14  |  8.1 KB  |  245 lines

  1. VERSION 5.00
  2. Begin VB.Form frmQuerys 
  3.    Caption         =   "Querys"
  4.    ClientHeight    =   4185
  5.    ClientLeft      =   1650
  6.    ClientTop       =   1545
  7.    ClientWidth     =   5100
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   4185
  10.    ScaleWidth      =   5100
  11.    Tag             =   "Querys"
  12.    Begin VB.ListBox lstQueryDefs 
  13.       Height          =   1260
  14.       Left            =   96
  15.       TabIndex        =   0
  16.       Top             =   274
  17.       Width           =   3392
  18.    End
  19.    Begin VB.TextBox txtSQLStatement 
  20.       BackColor       =   &H00FFFFFF&
  21.       Height          =   2159
  22.       Left            =   96
  23.       MultiLine       =   -1  'True
  24.       ScrollBars      =   2  'Vertical
  25.       TabIndex        =   4
  26.       Top             =   1921
  27.       Width           =   4931
  28.    End
  29.    Begin VB.CommandButton cmdRemoveQuery 
  30.       Caption         =   "&Remove"
  31.       Height          =   370
  32.       Left            =   3572
  33.       TabIndex        =   3
  34.       Tag             =   "&Remove"
  35.       Top             =   1277
  36.       Width           =   1443
  37.    End
  38.    Begin VB.CommandButton cmdSaveQueryDef 
  39.       Caption         =   "&Save"
  40.       Height          =   370
  41.       Left            =   3572
  42.       TabIndex        =   2
  43.       Tag             =   "&Save"
  44.       Top             =   775
  45.       Width           =   1443
  46.    End
  47.    Begin VB.CommandButton cmdExecuteSQL 
  48.       Caption         =   "&Execute"
  49.       Enabled         =   0   'False
  50.       Height          =   370
  51.       Left            =   3572
  52.       TabIndex        =   1
  53.       Tag             =   "&Execute"
  54.       Top             =   274
  55.       Width           =   1443
  56.    End
  57.    Begin VB.Label lblSQL 
  58.       Caption         =   "SQL Statement:"
  59.       Height          =   251
  60.       Index           =   1
  61.       Left            =   132
  62.       TabIndex        =   6
  63.       Tag             =   "SQL Statement:"
  64.       Top             =   1682
  65.       Width           =   2189
  66.    End
  67.    Begin VB.Label lblSQL 
  68.       Caption         =   "Saved Querys:"
  69.       Height          =   251
  70.       Index           =   0
  71.       Left            =   108
  72.       TabIndex        =   5
  73.       Tag             =   "Saved Querys:"
  74.       Top             =   24
  75.       Width           =   2189
  76.    End
  77. Attribute VB_Name = "frmQuerys"
  78. Attribute VB_GlobalNameSpace = False
  79. Attribute VB_Creatable = False
  80. Attribute VB_PredeclaredId = True
  81. Attribute VB_Exposed = False
  82. Option Explicit
  83. '====================================================================
  84. 'this template requires the following code (or it's equivalent)
  85. 'to be present in the application as well as a reference to DAO 3.50
  86. 'and the DataGrid template
  87. 'Global gsDatabase As String
  88. 'Global gsRecordsource As String
  89. 'Sub Main()
  90. '  gsDatabase = "c:\vb5\biblio.mdb"
  91. '  frmQuerys.Show
  92. 'End Sub
  93. '====================================================================
  94. Dim mdbDatabase As Database
  95. Private Sub Form_Load()
  96.     Set mdbDatabase = OpenDatabase(gsDatabase)
  97.     RefreshQuerys
  98.     Me.Left = GetSetting(App.Title, "Settings", "QueryLeft", 0)
  99.     Me.Top = GetSetting(App.Title, "Settings", "QueryTop", 0)
  100. End Sub
  101. Private Sub Form_Unload(Cancel As Integer)
  102.     If Me.WindowState <> vbMinimized Then
  103.         SaveSetting App.Title, "Settings", "QueryLeft", Me.Left
  104.         SaveSetting App.Title, "Settings", "QueryTop", Me.Top
  105.     End If
  106. End Sub
  107. Private Sub cmdSaveQueryDef_Click()
  108.     On Error GoTo SQDErr
  109.     Dim sQueryName As String
  110.     Dim sTmp As String
  111.     Dim qdNew As QueryDef
  112.     If lstQueryDefs.ListIndex >= 0 Then
  113.         'a querydef is selected so user may want to update it's SQL
  114.         If MsgBox("Update '" & lstQueryDefs.Text & "'?", vbYesNo + vbQuestion) = vbYes Then
  115.             'store the SQL from the SQL Window in the currently
  116.             'selected querydef
  117.             mdbDatabase.QueryDefs(lstQueryDefs.Text).SQL = Me.txtSQLStatement.Text
  118.             Exit Sub
  119.         End If
  120.     End If
  121.     'either there is no current querydef selected or the user
  122.     'didn't want to update the current one so we need
  123.     'to propmpt for a new name
  124.     sQueryName = InputBox("Enter New Query Name:")
  125.     If Len(sQueryName) = 0 Then Exit Sub
  126.     'add the new querydef
  127.     Set qdNew = mdbDatabase.CreateQueryDef(sQueryName)
  128.     'prompt for passthrough querydef
  129.     If MsgBox("Is this a SQLPassThrough QueryDef?", vbYesNo + vbQuestion + vbDefaultButton2) = vbYes Then
  130.         sTmp = InputBox("Enter Connect property value:")
  131.         If Len(sTmp) > 0 Then
  132.             qdNew.Connect = sTmp
  133.             If MsgBox("Is the Query Row Returning?", vbYesNo + vbQuestion) = vbNo Then
  134.                 qdNew.ReturnsRecords = False
  135.             End If
  136.         End If
  137.     End If
  138.     qdNew.SQL = txtSQLStatement.Text
  139.     mdbDatabase.QueryDefs.Refresh
  140.     RefreshQuerys
  141.     Exit Sub
  142. SQDErr:
  143.     MsgBox Err.Description
  144. End Sub
  145. Private Sub lstQueryDefs_Click()
  146.     txtSQLStatement.Text = mdbDatabase.QueryDefs(lstQueryDefs.Text).SQL
  147. End Sub
  148. Private Sub lstQueryDefs_DblClick()
  149.     cmdExecuteSQL_Click
  150. End Sub
  151. Private Sub txtSQLStatement_Change()
  152.     If Len(txtSQLStatement.Text) > 0 Then
  153.         cmdExecuteSQL.Enabled = True
  154.     Else
  155.         cmdExecuteSQL.Enabled = False
  156.     End If
  157. End Sub
  158. Private Sub cmdExecuteSQL_Click()
  159.     Dim rsTmp As Recordset
  160.     Dim dbTmp As Database
  161.     Dim qdfTmp As QueryDef
  162.     Dim bSavedQDF As Boolean
  163.     Dim sSQL As String
  164.     If Len(txtSQLStatement.Text) = 0 Then Exit Sub
  165.     Set dbTmp = OpenDatabase(gsDatabase)
  166.     If lstQueryDefs.ListIndex >= 0 Then
  167.         sSQL = dbTmp.QueryDefs(lstQueryDefs.Text).SQL
  168.         If sSQL = txtSQLStatement.Text Then
  169.             Set qdfTmp = dbTmp.QueryDefs(lstQueryDefs.Text)
  170.             bSavedQDF = True
  171.             If Not SetQryParams(qdfTmp) Then Exit Sub
  172.         Else
  173.             'just create a temp querydef
  174.             Set qdfTmp = dbTmp.CreateQueryDef(vbNullString, txtSQLStatement.Text)
  175.         End If
  176.     Else
  177.         'just create a temp querydef
  178.         Set qdfTmp = dbTmp.CreateQueryDef(vbNullString, txtSQLStatement.Text)
  179.     End If
  180.     Screen.MousePointer = vbHourglass
  181.     If UCase(Mid(txtSQLStatement, 1, 6)) = "SELECT" And InStr(UCase(txtSQLStatement.Text), " INTO ") = 0 Then
  182.         On Error GoTo SQLErr
  183. MakeDynaset:
  184.         Dim f As New frmDataGrid
  185.         Set rsTmp = qdfTmp.OpenRecordset()
  186.         Set f.Data1.Recordset = rsTmp
  187.         If bSavedQDF Then
  188.             f.Caption = qdfTmp.Name
  189.         Else
  190.             f.Caption = Left(txtSQLStatement.Text, 32) & "..."
  191.         End If
  192.         f.Show
  193.     Else
  194.         On Error GoTo SQLErr
  195.         qdfTmp.Execute
  196.     End If
  197.     Screen.MousePointer = vbDefault
  198.     Exit Sub
  199. SQLErr:
  200.     If Err = 3065 Or Err = 3078 Then 'row returning or name not found so try to create recordset
  201.         Resume MakeDynaset
  202.     End If
  203.     MsgBox Err.Description
  204. SQLEnd:
  205. End Sub
  206. Private Sub Form_Resize()
  207.     On Error Resume Next
  208.     If WindowState <> 1 Then
  209.         If Me.Width < 5220 Then Me.Width = 5220
  210.         If Me.Height < 2784 Then Me.Height = 2784
  211.         
  212.         txtSQLStatement.Width = Me.Width - 320
  213.         txtSQLStatement.Height = Me.Height - 2424
  214.     End If
  215. End Sub
  216. Sub RefreshQuerys()
  217.     Dim qdf As QueryDef
  218.     lstQueryDefs.Clear
  219.     For Each qdf In mdbDatabase.QueryDefs
  220.         lstQueryDefs.AddItem qdf.Name
  221.     Next
  222. End Sub
  223. Private Function SetQryParams(rqdf As QueryDef) As Boolean
  224.     On Error GoTo SPErr
  225.     Dim prm As Parameter
  226.     Dim sTmp As String
  227.     Dim i As Integer
  228.     For Each prm In rqdf.Parameters
  229.         'get the value from the user
  230.         sTmp = InputBox("Enter Value for Parameter '" & prm.Name & "':")
  231.         If Len(sTmp) = 0 Then
  232.             'bail out if the user doesn't enter one of the params
  233.             SetQryParams = False
  234.             Exit Function
  235.         End If
  236.         'store the value
  237.         prm.Value = CVar(sTmp)
  238.     Next
  239.     SetQryParams = True
  240.     Exit Function
  241.         
  242. SPErr:
  243.     MsgBox Err.Description
  244. End Function
  245.